<template>
	<view style="height: 100%">
		<view v-if="coupons.length > 0" class="couponList">
			<view class="coupon_item" :data-item="item" @tap="showDetail" v-for="(item, i) in coupons" :key="i">
				<view class="top">
					<view class="left_item algin">
						<view class="money">
							<text>￥</text>
							<text class="num">{{ item.Ffacevalue }}</text>
						</view>
					</view>
					<view class="center_item algin">
						<view class="text">消费满￥{{ item.Ffullvalue }}抵用</view>
						<view class="date">还有{{ item.Flastday }}天到期</view>
					</view>
					<view v-if="isorder == 1" class="right_item algin">
						<text :data-info="item" @tap="use" class="use_btn">立即使用</text>
					</view>
				</view>

				<view class="next">
					{{ item.Fsummary }}
				</view>
			</view>
		</view>
		<view v-else class="blank">
			<view class="algin">
				<image mode="widthFix" class="noImg" src="/static/file/images/nolist.png"></image>
			</view>
			<view @tap="addAddress" class="no_msg algin">暂无优惠劵</view>
		</view>
		<view v-if="isorder == 0" class="algin bottom">
			<navigator url="/pages/myCouponed/myCoupon" hover-class="none">查看已失效的劵></navigator>
		</view>
	</view>
</template>

<script>
	// pages/myCoupon/myCoupon.js
	var util = require('../../utils/util.js');
	var api = require('../../utils/prom.js');
	export default {
		data() {
			return {
				color: app.globalData.color,
				coupons: [],
				isorder: 0,
				proList: [],
				i: ''
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function(options) {
			var proList = options.proList;
			if (!util.isEmpty(proList)) {
				var arr = [];
				proList.map(function(item) {
					arr.push({
						Fpid: item.Fspuid,
						Fnum: item.Fnum,
						Fprice: item.Fsaleprice
					});
				});
				this.isorder = 1;
				this.proList = arr;
			}
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			var that = this;
			if (that.isorder == 1) {
				api.request('SpCouponUser?token=' + uni.getStorageSync('token'), 'post', that.proList, function(res) {
					that.coupons = res.data.Rows;
				});
			} else {
				var param = {};
				api.request('SpCouponUser?token=' + uni.getStorageSync('token') + '&status=1', 'get', param, function(
					res) {
					that.coupons = res.data.Rows;
				});
			}
		},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {
			return {
				title: app.globalData.shareinfo.title,
				imageUrl: app.globalData.shareinfo.img,
				desc: app.globalData.shareinfo.desc,
				path: 'pages/index/index?Fid=' + uni.getStorageSync('uid')
				//点击分享的图片进到哪一个页面
			};
		},
		methods: {
			use: function(e) {
				var data = e.currentTarget.dataset.info;
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2]; //上一个页面
				prevPage.setCoupon(data);
				//返回上一页
				uni.navigateBack({
					delta: 1
				});
			},

			showDetail() {
				console.log('占位：函数 showDetail 未声明');
			},

			addAddress() {
				console.log('占位：函数 addAddress 未声明');
			}
		}
	};
</script>
<style>
	@import './myCoupon.css';
</style>